var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vipcurrt: 0,
    payprice: 0,
    payhid: 0,
    currentTab: 0,
  },
  handleCardTap(e) {
    var that = this;
    var vipcurrt = e.currentTarget.dataset.dex;
    var huiyuanlist = that.data.huiyuanlist;
    console.log(vipcurrt)
    console.log(huiyuanlist);
    that.setData({
      vipcurrt: vipcurrt,
      payprice: huiyuanlist[vipcurrt].price,
      payhid: huiyuanlist[vipcurrt].id,
    })
  },
  topNavChange: function (e) {
    var that = this;
    var nextActiveIndex = e.currentTarget.dataset.current; //下标
    var payprice = that.data.huiyuanlist[nextActiveIndex].price;
    var payhid = that.data.huiyuanlist[nextActiveIndex].id;
    console.log(payprice, payhid)
    that.setData({
      currentTab: nextActiveIndex,
      payprice: payprice,
      payhid: payhid,
    });
  },
  paybtn() {
    var that = this;
    var payprice = that.data.payprice;
    var tkid = app.globalData.tkid //一级
    var mytkid = app.globalData.mytkid //二级
    var ziji = app.globalData.ziji //查询当前是不是自己是一级分销商
    console.log(tkid, mytkid, ziji)
    var payhid = that.data.payhid;
    var type = that.data.type;
    if(payprice != 0 && payprice != '0.00' && payprice != '0' && payprice != '')
    {
      app.util.request({
        'url': 'entry/wxapp/huiyuan.pay',
        header: {
          "Content-Type": "application/xml"
        },
        method: "GET",
        data: {
          openid: wx.getStorageSync('openid'),
          payprice: payprice,
        },
        success: function (res) {
          var ordersn = res.data.ordersn;
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr, //字符串随机数
            'package': res.data.package,
            'signType': res.data.signType,
            'paySign': res.data.paySign,
            success: function (res) {},
            complete: function (res) {
              console.log(res)
              if (res.errMsg != 'requestPayment:fail cancel') {
                app.util.request({
                  'url': 'entry/wxapp/huiyuan.payaddhuiyuan',
                  data: {
                    openid: wx.getStorageSync('openid'),
                    payhid: payhid,
                    type: type,
                    ordersn: ordersn
                  },
                  success: function (payres) {
                    wx.showToast({
                      title: '支付成功!',
                    })
                    var id = payres.data.id
                    var orders = payres.data.orders
  
                    //提醒
                    app.util.request({
                      'url': 'entry/wxapp/user.memberopentx',
                      data: {
                        near: '会员开通提醒',
                        openid:wx.getStorageSync('openid'),
                      },
                      success: function (res) {
                        console.log(res)
                      }
                    })
  
                    app.util.request({
                      'url': 'entry/wxapp/tuike.uptopmoney',
                      data: {
                        tkid: tkid,
                        mytkid: mytkid,
                        money: payprice,
                        openid: wx.getStorageSync('openid'),
                        leixing: 'vip',
                        orders: orders
                      },
                      success: function (res) {
                        console.log(res)
                      }
                    })
                    setTimeout(function () {
                      if (type != 2) {
                        wx.navigateBack({
                          delta: 2
                        })
                      } else {
                        var pages = getCurrentPages();
                        var prevPage = pages[pages.length - 2]; //上一个页面
                        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
                        // console.log(payres)
                        prevPage.setData({
                          buyId: id,
                        })
                        wx.navigateBack({
                          delta: 1
                        })
                      }
                    }, 1000)
                  },
                })
              }
            }
          })
        }
      })
    }else{
      app.util.request({
        'url': 'entry/wxapp/huiyuan.payaddhuiyuan',
        data: {
          openid: wx.getStorageSync('openid'),
          payhid: payhid,
          type: type,
          // ordersn: ordersn
        },
        success: function (payres) {
          wx.showToast({
            title: '支付成功!',
          })
          var id = payres.data.id
          var orders = payres.data.orders

          //提醒
          app.util.request({
            'url': 'entry/wxapp/user.memberopentx',
            data: {
              near: '会员开通提醒',
              openid:wx.getStorageSync('openid'),
            },
            success: function (res) {
              console.log(res)
            }
          })

          app.util.request({
            'url': 'entry/wxapp/tuike.uptopmoney',
            data: {
              tkid: tkid,
              mytkid: mytkid,
              money: payprice,
              openid: wx.getStorageSync('openid'),
              leixing: 'vip',
              orders: orders
            },
            success: function (res) {
              console.log(res)
            }
          })
          setTimeout(function () {
            if (type != 2) {
              wx.navigateBack({
                delta: 2
              })
            } else {
              var pages = getCurrentPages();
              var prevPage = pages[pages.length - 2]; //上一个页面
              //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
              // console.log(payres)
              prevPage.setData({
                buyId: id,
              })
              wx.navigateBack({
                delta: 1
              })
            }
          }, 1000)
        },
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      type: options.type
    })
    var currentTab = options.currentTab;
    if (currentTab != undefined) {
      that.setData({
        currentTab: currentTab
      })
    }
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.getAllhuiyuan();
  },
  getAllhuiyuan: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/huiyuan.allhuiyuan',
      data: {
        openid: wx.getStorageSync('openid'),
      },
      success: function (res) {
        if (that.data.vipcurrt == 0) {
          that.setData({
            payprice: res.data[0].price,
            payhid: res.data[0].id,
          })
        }
        if (that.data.currentTab != undefined) {
          that.setData({
            payprice: res.data[that.data.currentTab].price,
            payhid: res.data[that.data.currentTab].id,
          })
        }
        that.setData({
          huiyuanlist: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})